home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 41 / Amiga Format CD41 (1999-06)(Future Publishing)(GB)[!][issue 1999-07].iso / -seriously_amiga- / misc / amised / viewers / sources / c_viewer.c < prev    next >
C/C++ Source or Header  |  1999-04-19  |  20KB  |  968 lines

  1. /*
  2.  
  3.  Viewer       : C.
  4.  Version      : 1.00.
  5.  Date         : September 7, 1997.
  6.  Distribution : freeware.
  7.  Author(s)    : Willem Mestrom.
  8.  
  9.  Comment      : This is the complete source code of the C-Source viewer. It
  10.                 may be useful when you want to create your own viewer. Most
  11.                 of the important options of AMIS are used so you can see
  12.                 how it all works.
  13.  
  14. */
  15.  
  16. #include <exec/types.h>
  17. #include <clib/graphics_protos.h>
  18. #include "viewer.h"
  19. #include "AmisED:Include/amis_lib.h"
  20. #include "AmisED:Include/amis_pragmas.h"
  21.  
  22. void initialize(void);
  23. long __saveds __asm Display(register __a0 char *text,
  24.                                         register __a1 char *buf,
  25.                                         register __d0 long length,
  26.                                         register __a5 struct EditWin *editwin);
  27. long __saveds __asm Init(register __a0 char *text,
  28.                                     register __a1 char *buf,
  29.                                     register __d0 long length,
  30.                                     register __a5 struct EditWin *editwin);
  31. void __saveds SwitchFunc(void);
  32. void MyPrint(struct EditWin *editwin,char *text,long length,long color);
  33.  
  34. void __saveds screen_opens(void);
  35. void __saveds screen_closes(void);
  36.  
  37. long __saveds __asm drawcurs(register __d0 long x1,
  38.                                         register __d1 long y1,
  39.                                         register __d2 long x2,
  40.                                         register __d3 long y2,
  41.                                         register __a5 struct EditWin *editwin);
  42.  
  43. void __saveds Quit(void);
  44.  
  45. void __saveds Preferences(void);
  46. void __saveds ClosePrefs(void);
  47. void __saveds LoadPrefs(void);
  48. void __saveds SavePrefs(void);
  49.  
  50. void __saveds listselect(void);
  51. void __saveds change_bold(void);
  52. void __saveds change_italic(void);
  53. void __saveds change_underline(void);
  54. void __saveds change_color(void);
  55.  
  56. void DrawNewCurs(struct EditWin *editwin,long c,char *line,
  57.                         long x1,long y1,long x2,long y2);
  58.  
  59. void __saveds __asm make_palgg(register __a5 struct AMIS_Requester *req);
  60. void __saveds __asm layout_buttons(register __a5 struct AMIS_Requester *req);
  61.  
  62. int TextChar(char sign);
  63.  
  64. void __stdargs _XCEXIT(long);
  65.  
  66. struct List C_keylist;
  67. struct List C_indlist;
  68. struct List C_keylist = {
  69.     (struct Node *)((char *)&C_keylist+4),
  70.     0,
  71.     (struct Node *)&C_keylist
  72. };
  73. struct List C_indlist = {
  74.     (struct Node *)((char *)&C_indlist+4),
  75.     0,
  76.     (struct Node *)&C_indlist
  77. };
  78.  
  79. /*struct GfxBase *GfxBase;
  80. struct Library *GadToolsBase;
  81. struct DosLibrary *DOSBase;
  82. struct IntuitionBase *IntuitionBase;*/
  83. struct AMIS *AMIS;
  84. struct AMIS *ViewerLib;
  85. struct Library *AMISLibBase;
  86.  
  87. struct AMIS_Requester *req;
  88.  
  89. struct AMIS_ReqDef prefs_req={-1,25,392,12*256+27,0,0,0,
  90.     "C-Source settings..."};
  91.  
  92. struct List symbol_list;
  93.  
  94. struct Node symbol_nodes[10] = {
  95.     {
  96.         &symbol_nodes[1],
  97.         (struct Node *)&symbol_list,
  98.         1,0,
  99.         "Normal text"
  100.     },
  101.     {
  102.         &symbol_nodes[2],
  103.         &symbol_nodes[0],
  104.         2,0,
  105.         "ANSI C/C++ Comment"
  106.     },
  107.     {
  108.         &symbol_nodes[3],
  109.         &symbol_nodes[1],
  110.         4,0,
  111.         "Single quoted text"
  112.     },
  113.     {
  114.         &symbol_nodes[4],
  115.         &symbol_nodes[2],
  116.         3,0,
  117.         "Double quoted text"
  118.     },
  119.     {
  120.         &symbol_nodes[5],
  121.         &symbol_nodes[3],
  122.         8,0,
  123.         "{ and }"
  124.     },
  125.     {
  126.         &symbol_nodes[6],
  127.         &symbol_nodes[4],
  128.         9,0,
  129.         ". and ->"
  130.     },
  131.     {
  132.         &symbol_nodes[7],
  133.         &symbol_nodes[5],
  134.         6,0,
  135.         "ANSI C keywords"
  136.     },
  137.     {
  138.         &symbol_nodes[8],
  139.         &symbol_nodes[6],
  140.         5,0,
  141.         "ANSI C symbols"
  142.     },
  143.     {
  144.         &symbol_nodes[9],
  145.         &symbol_nodes[7],
  146.         7,0,
  147.         "Preprocessor commands"
  148.     },
  149.     {
  150.         (struct Node *)(((long)&symbol_list)+4),
  151.         &symbol_nodes[8],
  152.         10,0,
  153.         "User dictionary 1"
  154.     }    
  155. };
  156.  
  157. struct List symbol_list = {
  158.     &symbol_nodes[0],
  159.     0,
  160.     &symbol_nodes[9]
  161. };
  162.  
  163. char bold_flag=0,italic_flag=0,underline_flag=0;
  164.  
  165. struct AMIS_Button prefs_ok =
  166.     {8,11*256+19,100,1*256+6,"_Ok",0,BUTTON_KIND,
  167.        -1,ClosePrefs};
  168.  
  169. struct AMIS_Button prefs_load =
  170.     {8,11*256+19,100,1*256+6,"_Load",0,BUTTON_KIND,
  171.        -2,LoadPrefs};
  172.  
  173. struct AMIS_Button prefs_save =
  174.     {8,11*256+19,100,1*256+6,"_Save",0,BUTTON_KIND,
  175.        -3,SavePrefs};
  176.  
  177. struct AMIS_CheckMark prefs_bold =
  178.     {222,1*256+10,26,1*256+3,"_Bold",0,CHECKBOX_KIND,
  179.        2,&bold_flag,0,FSF_BOLD,change_bold};
  180.  
  181. struct AMIS_CheckMark prefs_italic =
  182.     {222,2*256+13,26,1*256+3,"_Italic",0,CHECKBOX_KIND,
  183.        3,&italic_flag,0,FSF_ITALIC,change_italic};
  184.  
  185. struct AMIS_CheckMark prefs_underline =
  186.     {222,3*256+16,26,1*256+3,"_Underline",0,CHECKBOX_KIND,
  187.        4,&underline_flag,0,FSF_UNDERLINED,change_underline};
  188.  
  189. struct AMIS_Listview prefs_list =
  190.     {14,1*256+10,200,10*256+4,"_Symbols",0,LISTVIEW_KIND,
  191.        1,&symbol_list,0,0,listselect,0,0,0};
  192.  
  193. struct AMIS_GText prefs_pal =
  194.     {222,4*256+21,164,1*256+6,"",0,TEXT_KIND,
  195.        5,0,change_color};
  196.  
  197. struct AMIS_Backbox prefs_backbox =
  198.     {8,2,384,11*256+15};
  199.  
  200. ULONG prefs_gadgets[]={
  201.     AM_Listview,(ULONG)&prefs_list,
  202.     AM_GText,(ULONG)&prefs_pal,
  203.     AM_Code,(ULONG)make_palgg,
  204.     AM_UserGadget,0,
  205.     AM_CheckMark,(ULONG)&prefs_bold,
  206.     AM_CheckMark,(ULONG)&prefs_italic,
  207.     AM_CheckMark,(ULONG)&prefs_underline,
  208.     AM_SameWidth,0x00500003,
  209.     AM_Code,(ULONG)layout_buttons,
  210.     AM_Button,(ULONG)&prefs_ok,
  211.     AM_Button,(ULONG)&prefs_load,
  212.     AM_Button,(ULONG)&prefs_save,
  213.     AM_BackBox,(ULONG)&prefs_backbox,
  214.     TAG_END
  215. };
  216.  
  217. ULONG prefs_tags[]={
  218.     AM_Screen,0,
  219.     AM_MessagePort,0,
  220.     AM_Gadgets,(ULONG)&prefs_gadgets[0],
  221.     AM_EscFunction,(ULONG)ClosePrefs,
  222.     AM_LocaleOffset,0x100000,
  223.     TAG_END
  224. };
  225.  
  226. struct Gadget *dummy_gg;
  227.  
  228. #define    COLORS        11
  229.  
  230. /*
  231.     color 1:  normal text
  232.     color 2:  comment
  233.     color 3:  double-quoted text ("text")
  234.     color 4:  single-quoted text ('text')
  235.     color 5:  c/c++ symbols
  236.     color 6:  c/c++ keywords
  237.     color 7:  preprocessor keywords (example: #include)
  238.     color 8:  '{' and '}'
  239.     color 9:  '->' and '.'
  240.     color 10: User dictionary 1
  241. */
  242.  
  243. struct color {
  244.     long    color;
  245.     char    flags;
  246.     char    dummy;
  247. };
  248.  
  249. struct color colorlist[COLORS] = {
  250.     {0,0},{1,0},{2,0},{2,0},{3,0},{3,FSF_BOLD},{2,0},{1,0},{3,0},{7,0},{2,0}
  251. };
  252.  
  253. char *keywordlist1[]={
  254.     "struct",        /* C/C++ symbols */
  255.     "char",
  256.     "int",
  257.     "short",
  258.     "long",
  259.     "float",
  260.     "double",
  261.     "unsigned",
  262.     "void",
  263.     "Object",
  264.     "VOID",
  265.     "BOOL",
  266.     "FLOAT",
  267.     "DOUBLE",
  268.     "TEXT",
  269.     "BYTE",
  270.     "UBYTE",
  271.     "BYTEBITS",
  272.     "BYTEMASK",
  273.     "WORD",
  274.     "UWORD",
  275.     "WORDBITS",
  276.     "LONG",
  277.     "ULONG",
  278.     "LONGBITS",
  279.     "SHORT",
  280.     "USHORT",
  281.     "COUNT",
  282.     "UCOUNT",
  283.     "APTR",
  284.     "BPTR",
  285.     "CPTR",
  286.     "RPTR",
  287.     "STRPTR",
  288.     "TRUE",
  289.     "FALSE",
  290.     "__saveds",
  291.     "__stdargs",
  292.     "__asm",
  293.     "register",
  294.     "__d0",
  295.     "__d1",
  296.     "__d2",
  297.     "__d3",
  298.     "__d4",
  299.     "__d5",
  300.     "__d6",
  301.     "__d7",
  302.     "__a0",
  303.     "__a1",
  304.     "__a2",
  305.     "__a3",
  306.     "__a4",
  307.     "__a5",
  308.     "__a6",
  309.     "__a7",
  310.     "__fp0",
  311.     "__fp1",
  312.     "__fp2",
  313.     "__fp3",
  314.     "__fp4",
  315.     "__fp5",
  316.     "__fp6",
  317.     "__fp7",
  318.     "GLOBAL",
  319.     "IMPORT",
  320.     "STATIC",
  321.     "REGISTER",
  322.     "typedef",
  323.     "auto",
  324.     "enum",
  325.     "extern",
  326.     "static",
  327.     "union",
  328.     "class",
  329.     "friend",
  330.     "inline",
  331.     "operator",
  332.     "overload",
  333.     "public",
  334.     "this",
  335.     "virtual",
  336.     0
  337. };
  338.  
  339. char *keywordlist2[]={
  340.     "if",
  341.     "else",
  342.     "switch",
  343.     "case",
  344.     "default",
  345.     "break",
  346.     "while",
  347.     "for",
  348.     "return",
  349.     "goto",
  350.     "continue",
  351.     "do",
  352.     "delete",
  353.     "new",
  354.     "sizeof",
  355.     0
  356. };
  357.  
  358. struct Viewer C_Viewer={
  359.     0,0,                                /* must be zero */
  360.     AMIS_1_03,                        /* the version of AMIS which you need at least
  361.                                             to use this viewer */
  362.     0,                                    /* must be zero */
  363.     "C-Source",                        /* name of the viewer */
  364.     0,                                    /* must be zero */
  365.     "(#?.c|#?.h|#?.cpp|#?.cxx|#?.c++)",
  366.                                         /* pattern of files to view with this viewer */
  367.     0,                                    /* no special text at the start of a file */
  368.     0,                                    /* no special text in file to recognize it */
  369.     0,                                    /* no switch function */
  370.     (1<<VFLG1_INIT)+(1<<VFLG1_PRINTINIT),0,
  371.                                         /* flags */
  372.     Display,                            /* the display function */
  373.     0,0,0,0,                            /* standard control functions */
  374.     1,0,0,0,7,0,                    /* normal lines */
  375.     0,0,0,0,0,0,                    /* standard control functions */
  376.     &C_keylist,                        /* special keylist */
  377.     0,0,                                /* standard control functions */
  378.     Quit,
  379.     "C-Source viewer v1.31||Written by Willem Mestrom & Vincent Groenewold.",
  380.     0,
  381.     screen_opens,
  382.     screen_closes,
  383.     drawcurs,
  384.     0,0,0,0,
  385.     Preferences,
  386.     Init,
  387.     &C_indlist                        /* special indlist */
  388. };
  389.  
  390. long leftoffset,printed,init=0,initialized=FALSE;
  391.  
  392. struct RB_Tree dictionary={0,0};
  393. struct RB_Tree keylist1={0,0};
  394. struct RB_Tree keylist2={0,0};
  395.  
  396. char *dicbuf,*ver="$VER: C-Viewer 1.31 (17.9.97)";
  397.  
  398. struct Viewer __saveds __asm *InitViewer(register __a0 struct AMIS *NewAMIS)
  399. {
  400.     AMIS=NewAMIS;
  401.     ViewerLib=AMIS;
  402.     AMISLibBase=AMIS->AMIS_AMISLibBase;
  403.     C_Viewer.vw_font=AMIS->AMIS_Normal_Font;
  404.     GfxBase=AMIS->AMIS_GfxBase;
  405.     GadToolsBase=AMIS->AMIS_GadToolsBase;
  406.     DOSBase=AMIS->AMIS_DOSBase;
  407.     IntuitionBase=AMIS->AMIS_IntuitionBase;
  408.     prefs_tags[3]=(ULONG)AMIS->AMIS_MsgPort;
  409.     return(&C_Viewer);
  410. }
  411.  
  412. /*#include "Programming:AmisC/include/HashTable.h"*/
  413.  
  414. void initialize()
  415. {
  416.     long pf,c,i;
  417.     struct FileInfoBlock *fib;
  418.     struct RB_Node *word;
  419.  
  420.     InternalCommand("Message \"Building symbol list...\"");
  421.     pf=Open("PROGDIR:Viewers/Prefs/C-Viewer.dic",MODE_OLDFILE);
  422.     if(pf!=0)
  423.     {
  424.         if(fib=(struct FileInfoBlock *)AllocDosObject(DOS_FIB,0))
  425.         {
  426.             ExamineFH(pf,fib);
  427.             if(dicbuf=(char *)AllocVec(fib->fib_Size,MEMF_PUBLIC))
  428.             {
  429.                 Read(pf,dicbuf,fib->fib_Size);
  430.                 i=0;c=0;
  431.                 while(i<fib->fib_Size)
  432.                     if(dicbuf[i++]==10)
  433.                     {
  434.                         dicbuf[i-1]=0;
  435.                         if(c<(i-1))
  436.                             if(word=RB_NewNode(&dicbuf[c]))
  437.                                 RB_InsertNode(&dictionary,word);
  438.                         c=i;
  439.                     }
  440.             }
  441.             FreeDosObject(DOS_FIB,fib);
  442.         }
  443.         Close(pf);
  444.     }
  445.     RB_BuiltTree(&keylist1,(APTR *)keywordlist1);
  446.     RB_BuiltTree(&keylist2,(APTR *)keywordlist2);
  447.     LoadPrefs();
  448.     initialized=TRUE;
  449. }
  450.  
  451. void __saveds screen_opens()
  452. {
  453.     prefs_tags[1]=(ULONG)AMIS->AMIS_ScreenBase;
  454. }
  455.  
  456. void __saveds screen_closes()
  457. {
  458.    struct Gadget *tg;
  459.     if(req!=0)
  460.     {
  461.         tg=prefs_list.gadget->NextGadget;
  462.         prefs_list.gadget=dummy_gg;
  463.         dummy_gg->NextGadget=tg;
  464.         FreeRequester(req);
  465.         DisposeObject((APTR)prefs_gadgets[7]);
  466.     }
  467.     req=0;
  468. }
  469.  
  470. /*        Example of using internal commands inside your own viewer
  471.         To activate this function just add it to the C_Viewer structure.
  472.  
  473. void __saveds SwitchFunc()
  474. {
  475.     AMIS_InternalCommand("Request BODY=\"test\"");
  476. }
  477.  
  478. */
  479.  
  480. void __saveds Quit()
  481. {
  482.     if(initialized==TRUE)
  483.     {
  484.         RB_Free(&dictionary);
  485.         RB_Free(&keylist1);
  486.         RB_Free(&keylist2);
  487.         FreeVec(dicbuf);
  488.     }
  489. }
  490.  
  491. long __saveds __asm Init(register __a0 char *text,
  492.                                     register __a1 char *buf,
  493.                                     register __d0 long length,
  494.                                     register __a5 struct EditWin *editwin)
  495. {
  496.     long result;
  497.  
  498.    init=1;
  499.    result=Display(text,buf,length,editwin);
  500.    init=0;
  501.    return result;
  502. }
  503.  
  504. long __saveds __asm Display(register __a0 char *text,
  505.                                         register __a1 char *buf,
  506.                                         register __d0 long length,
  507.                                         register __a5 struct EditWin *editwin)
  508. {
  509.     long    bufsign=0,charnum=0,tab,temp3,color;
  510.     char    sign,cppcomment=0;
  511.     int    i,j;
  512.     char    dtw;
  513.  
  514.     if(initialized==FALSE) initialize();
  515.  
  516.     leftoffset=-(editwin->ed_leftchar);
  517.     printed=0;
  518.  
  519.     color=editwin->ed_linesinfo[editwin->ed_line].li_viewerdata;
  520.     color=(color==0 ? 1 : color);
  521.  
  522.     SetAPen(editwin->wn_rast,1);
  523.     SetBPen(editwin->wn_rast,0);
  524.  
  525.     if((color==1)&&(text[0]=='#'))
  526.     {
  527.         buf[bufsign++]='#'; charnum++;
  528.         while((charnum<length)&&(text[charnum]!=' ')&&(text[charnum]!=9))
  529.             buf[bufsign++]=text[charnum++];
  530.         MyPrint(editwin,buf,bufsign,7);
  531.         bufsign=0;
  532.     }
  533.  
  534.     tab=(long)editwin->ed_tabsize;
  535.     if(tab==0) tab=8;
  536.     while(charnum<length)
  537.     {
  538.         sign=text[charnum-1];i=0;
  539.         if((color==1)&&(init==0)&&((charnum==0)||(TextChar(sign)==FALSE)))
  540.         {
  541.             if(TextChar(text[charnum])==TRUE)
  542.             {
  543.                 i=charnum;
  544.                 while(TextChar(text[++i])==TRUE);
  545.                 dtw=text[i];text[i]=0;
  546.                 if(RB_Search(&keylist1,&text[charnum])!=0)
  547.                 {
  548.                     text[i]=dtw;i-=charnum;j=1;
  549.                 }
  550.                 else if(RB_Search(&keylist2,&text[charnum])!=0)
  551.                 {
  552.                     text[i]=dtw;i-=charnum;j=2;
  553.                 }
  554.                 else
  555.                 {
  556.                     text[i]=dtw;i=0;j=0;
  557.                 }
  558.             }
  559.             if(i==0)
  560.             {
  561.                 if(TextChar(text[charnum])==TRUE)
  562.                 {
  563.                     i=charnum;
  564.                     while(TextChar(text[++i])==TRUE);
  565.                     dtw=text[i];text[i]=0;
  566.                     if(RB_Search(&dictionary,&text[charnum])!=0)
  567.                     {
  568.                         text[i]=dtw;i-=charnum;
  569.                     }
  570.                     else
  571.                     {
  572.                         text[i]=dtw;i=0;
  573.                     }
  574.                 }
  575.                 else
  576.                     i=0;
  577.                 if(i!=0)
  578.                 {
  579.                     MyPrint(editwin,buf,bufsign,color);bufsign=0;
  580.                     MyPrint(editwin,&text[charnum],i,10);
  581.                     charnum+=i;
  582.                 }
  583.             }
  584.             else
  585.            {
  586.                 MyPrint(editwin,buf,bufsign,color);bufsign=0;
  587.                 MyPrint(editwin,&text[charnum],i,(j==1 ? 5 : 6));
  588.                 charnum+=i;
  589.             }
  590.         }
  591.         if(i==0)
  592.         {
  593.             sign=text[charnum];
  594.             if((sign=='/')&&(text[charnum+1]=='/')&&((color==1)||(color==7)))
  595.             {
  596.                 MyPrint(editwin,buf,bufsign,color);
  597.                 color=2; bufsign=0; cppcomment=1;
  598.             }
  599.             if(((sign=='*')&&(text[charnum+1]=='/')&&(color==2))||
  600.                 ((sign=='"')&&(color==3))||
  601.                 ((sign=='\'')&&((text[charnum-1]!='\\')||
  602.                 ((text[charnum-1]=='\\')&&(text[charnum-2]=='\\')))&&(color==4)))
  603.             {
  604.                 if(sign=='*')
  605.                 {
  606.                     buf[bufsign]='*'; buf[bufsign+1]='/';
  607.                     MyPrint(editwin,buf,bufsign+2,color);
  608.                     color=1; bufsign=0; charnum+=2;
  609.                 }
  610.                 else
  611.                 {
  612.                     if(sign=='"')
  613.                     {
  614.                         long bscnt = 1;
  615.                         while( text[charnum-bscnt]=='\\' ) bscnt++;
  616.  
  617.                        if( bscnt&1 )
  618.                        {
  619.                             buf[bufsign]='"';
  620.                             MyPrint(editwin,buf,bufsign+1,color);
  621.                             color=1; bufsign=0; charnum++;
  622.                         }
  623.                         else
  624.                         {
  625.                             buf[bufsign++]='"';
  626.                             charnum++;
  627.                         }
  628.                     }
  629.                     else
  630.                     {
  631.                         buf[bufsign]='\'';
  632.                         MyPrint(editwin,buf,bufsign+1,color);
  633.                         color=1; bufsign=0; charnum++;
  634.                     }
  635.                 }
  636.             }
  637.             else
  638.             {
  639.                 if(((sign=='{')||(sign=='}'))&&(color==1))
  640.                 {
  641.                     MyPrint(editwin,buf,bufsign,color);
  642.                     bufsign=0; charnum++;
  643.                     MyPrint(editwin,&sign,1,8);
  644.                 }
  645.                 else
  646.                 {
  647.                     if((((sign=='/')&&(text[charnum+1]=='*'))||
  648.                             (sign=='"')||(sign=='\''))&&(color==1))
  649.                     {
  650.                         MyPrint(editwin,buf,bufsign,color);
  651.                         if(sign=='/')
  652.                         {
  653.                             buf[0]='/';buf[1]='*';bufsign=2;
  654.                             color=2; charnum+=2;
  655.                         }
  656.                         else
  657.                         {
  658.                             if(sign=='"')
  659.                             {
  660.                                 buf[0]='"';bufsign=1;
  661.                                 color=3; charnum++;
  662.                             }
  663.                             else
  664.                             {
  665.                                 buf[0]='\'';bufsign=1;
  666.                                 color=4; charnum++;
  667.                             }
  668.                         }
  669.                     }
  670.                     else
  671.                     {
  672.                         if(((sign=='.')||
  673.                             ((sign=='-')&&(text[charnum+1]=='>')))&&(color==1))
  674.                         {
  675.                             MyPrint(editwin,buf,bufsign,color);
  676.                             if(sign=='.')
  677.                             {
  678.                                 buf[0]='.';
  679.                                 bufsign=1;
  680.                             }
  681.                             else
  682.                             {
  683.                                 buf[0]='-';
  684.                                 buf[1]='>';
  685.                                 bufsign=2;
  686.                             }
  687.                             MyPrint(editwin,buf,bufsign,9);
  688.                             charnum+=bufsign; bufsign=0;
  689.                         }
  690.                         else
  691.                         {
  692.                             if(sign==9)
  693.                             {
  694.                                 temp3=(bufsign+printed)/tab;
  695.                                 temp3++;
  696.                                 temp3*=tab;
  697.                                 temp3-=(bufsign+printed);
  698.                                 while(temp3--)
  699.                                 {
  700.                                     buf[bufsign]=32;
  701.                                     bufsign++;
  702.                                 }
  703.                                 charnum++;
  704.                             }
  705.                             else
  706.                             {
  707.                                 buf[bufsign]=sign;
  708.                                 bufsign++;
  709.                                 charnum++;
  710.                             }
  711.                         }
  712.                     }
  713.                 }
  714.             }
  715.         }
  716.     }
  717.  
  718.     MyPrint(editwin,buf,bufsign,color);
  719.  
  720.     if(cppcomment)
  721.         color=0;
  722.  
  723.     editwin->ed_linesinfo[editwin->ed_line+1].li_viewerdata=(color==2 ? 2 : 1);
  724.  
  725.     return (-1);
  726. }
  727.  
  728. void MyPrint(struct EditWin *editwin,char *text,long length,long color)
  729. {
  730.     leftoffset+=length;printed+=length;
  731.     if((leftoffset>0)&&(init==0))
  732.     {
  733.        if(((editwin->ed_flags5)&(1<<EFLG5_FASTMODE))==0)
  734.        {
  735.            SetAPen(editwin->wn_rast,colorlist[color].color);
  736.            SetSoftStyle(editwin->wn_rast,colorlist[color].flags,-1);
  737.         }
  738.         Text(editwin->wn_rast,text+length-leftoffset,leftoffset);
  739.         leftoffset=0;
  740.     }
  741. }
  742.  
  743. void __saveds Preferences()
  744. {
  745.    if(!req)
  746.         req=BuiltRequester(&prefs_req,(struct TagItem *)&prefs_tags);
  747.     ShowRequester(req);
  748. }
  749.  
  750. void __saveds ClosePrefs()
  751. {
  752.     CloseRequester(req);
  753.     RedrawViewerWindows(&C_Viewer);
  754. }
  755.  
  756. void __saveds LoadPrefs(void)
  757. {
  758.     long pf;
  759.     pf=Open("PROGDIR:Viewers/Prefs/C_Viewer.prefs",MODE_OLDFILE);
  760.     Read(pf,&colorlist,COLORS*6);
  761.     Close(pf);
  762. }
  763.  
  764. void __saveds SavePrefs(void)
  765. {
  766.     long pf;
  767.     if(InternalCommand(
  768.     "Request BODY=\"Preferences will be saved|in Viewers/Prefs/C_Viewer.prefs\" BUTTON=\"_Ok|_Cancel\"")[0]=='1')
  769.     {
  770.         pf=Open("PROGDIR:Viewers/Prefs/C_Viewer.prefs",MODE_NEWFILE);
  771.         Write(pf,&colorlist,COLORS*6);
  772.         Close(pf);
  773.     }
  774. }
  775.  
  776. void __saveds listselect()
  777. {
  778.     long s=GetListview(prefs_list.gadget,req->ar_window);
  779.     long t=symbol_nodes[s].ln_Type;
  780.  
  781.     bold_flag=colorlist[t].flags&FSF_BOLD;
  782.     italic_flag=colorlist[t].flags&FSF_ITALIC;
  783.     underline_flag=colorlist[t].flags&FSF_UNDERLINED;
  784.     GT_SetGadgetAttrs(prefs_bold.gadget,req->ar_window,NULL,
  785.         GTCB_Checked,(bold_flag==0 ? 0:1),TAG_DONE);
  786.     GT_SetGadgetAttrs(prefs_italic.gadget,req->ar_window,NULL,
  787.         GTCB_Checked,(italic_flag==0 ? 0:1),TAG_DONE);
  788.     GT_SetGadgetAttrs(prefs_underline.gadget,req->ar_window,NULL,
  789.         GTCB_Checked,(underline_flag==0 ? 0:1),TAG_DONE);
  790.     SetGadgetAttrs((struct Gadget *)prefs_gadgets[7],req->ar_window,NULL,
  791.         PALGA_Color,colorlist[t].color,TAG_DONE);
  792. }
  793.  
  794. void __saveds change_bold()
  795. {
  796.     long s=GetListview(prefs_list.gadget,req->ar_window);
  797.     long t=symbol_nodes[s].ln_Type;
  798.     colorlist[t].flags=(colorlist[t].flags&(~FSF_BOLD))|bold_flag;
  799. }
  800.  
  801. void __saveds change_italic()
  802. {
  803.     long s=GetListview(prefs_list.gadget,req->ar_window);
  804.     long t=symbol_nodes[s].ln_Type;
  805.     colorlist[t].flags=(colorlist[t].flags&(~FSF_ITALIC))|italic_flag;
  806. }
  807.  
  808. void __saveds change_underline()
  809. {
  810.     long s=GetListview(prefs_list.gadget,req->ar_window);
  811.     long t=symbol_nodes[s].ln_Type;
  812.     colorlist[t].flags=(colorlist[t].flags&(~FSF_UNDERLINED))|underline_flag;
  813. }
  814.  
  815. void __saveds change_color()
  816. {
  817.     long s=GetListview(prefs_list.gadget,req->ar_window);
  818.     long t=symbol_nodes[s].ln_Type;
  819.     colorlist[t].flags=0;
  820.     colorlist[t].color=(long)*((char *)(prefs_gadgets[7]+70));
  821. }
  822.  
  823. long __saveds __asm drawcurs(register __d0 long x1,
  824.                                         register __d1 long y1,
  825.                                         register __d2 long x2,
  826.                                         register __d3 long y2,
  827.                                         register __a5 struct EditWin *editwin)
  828. {
  829.     char *linebuf;
  830.     char oc='(',tc;
  831.     long c=editwin->ed_char;
  832.     long cnt=1;
  833.     long l=editwin->ed_line;
  834.     long ll=editwin->ed_linebuflast;
  835.     long ol=1;
  836.  
  837.     linebuf=editwin->ed_linebuf;
  838.     if(((tc=linebuf[c])==')')||(tc=='}')||(tc==']'))
  839.     {
  840.        if(tc=='}')
  841.            oc='{';
  842.        if(tc==']')
  843.            oc='[';
  844.         while((l>=0)&&(y1>=editwin->ed_first_y))
  845.         {
  846.             while((--c)>=0)
  847.             {
  848.                 if(linebuf[c]==tc)
  849.                     cnt++;
  850.                 if(linebuf[c]==oc)
  851.                     if(--cnt==0)
  852.                     {
  853.                         DrawNewCurs(editwin,c,linebuf,x1,y1,x2,y2);
  854.                         return 0;
  855.                     }
  856.             }
  857.             if(ol==1)
  858.             {
  859.                 linebuf=editwin->ed_buffer+editwin->ed_bufpos;
  860.                 ol=0;
  861.             }
  862.             c=editwin->ed_linesinfo[--l].li_length;
  863.             linebuf-=c;
  864.             y1-=editwin->ed_lineheight;
  865.             y2-=editwin->ed_lineheight;
  866.         }
  867.     }
  868.     else if(((tc=linebuf[c])=='(')||(tc=='{')||(tc=='['))
  869.     {
  870.        oc=')';
  871.        if(tc=='{')
  872.            oc='}';
  873.        if(tc=='[')
  874.            oc=']';
  875.         while((l<=editwin->ed_lines)&&(y1<=editwin->ed_last_y))
  876.         {
  877.             while((++c)<ll)
  878.             {
  879.                 if(linebuf[c]==tc)
  880.                     cnt++;
  881.                 if(linebuf[c]==oc)
  882.                     if(--cnt==0)
  883.                     {
  884.                         DrawNewCurs(editwin,c,linebuf,x1,y1,x2,y2);
  885.                         return 0;
  886.                     }
  887.             }
  888.             if(ol==1)
  889.             {
  890.                 linebuf=editwin->ed_buffer+editwin->ed_bufpos+
  891.                             editwin->ed_oldlinelength+1;
  892.                 ol=0;
  893.             }
  894.             else
  895.                 linebuf+=ll;
  896.             ll=editwin->ed_linesinfo[++l].li_length;
  897.             y1+=editwin->ed_lineheight;
  898.             y2+=editwin->ed_lineheight;
  899.             c=-1;
  900.         }
  901.     }
  902.     return 0;
  903. }
  904.  
  905. void DrawNewCurs(struct EditWin *editwin,long c,char *line,
  906.                         long x1,long y1,long x2,long y2)
  907. {
  908.     long om=editwin->wn_rast->Mask;
  909.     long wp=0,cs=0;
  910.     long tab=(long)editwin->ed_tabsize;
  911.  
  912.     tab=(tab==0 ? 1 : tab);
  913.     while(cs<c)
  914.     {
  915.         if(line[cs++]==9)
  916.             wp=((wp/tab)+1)*tab;
  917.         else
  918.             wp++;
  919.     }
  920.    x2-=x1;
  921.    x1=editwin->ed_leftborder+
  922.            (wp-editwin->ed_leftchar)*editwin->wn_rast->TxWidth+
  923.            editwin->ed_cursoffset;
  924.    SetWriteMask(editwin->wn_rast,2);
  925.     RectFill(editwin->wn_rast,x1,y1,x1+x2,y2);
  926.    SetWriteMask(editwin->wn_rast,om);
  927. }
  928.  
  929. void __saveds __asm make_palgg(register __a5 struct AMIS_Requester *req)
  930. {
  931.     dummy_gg=req->ar_lastgadget;
  932.  
  933.     prefs_gadgets[7]=(ULONG)NewObject(AMIS->AMIS_PaletteClass,0,
  934.             GA_Left,dummy_gg->LeftEdge,
  935.             GA_Top,(dummy_gg->TopEdge)-(req->ar_topborder),
  936.             GA_Width,dummy_gg->Width,
  937.             GA_Height,dummy_gg->Height,
  938.             GA_ID,0,
  939.             GA_FollowMouse,1,
  940.             GA_Immediate,1,
  941.             GA_RelVerify,1,
  942.             PALGA_Colors,8,
  943.             TAG_END);
  944.     ((struct Gadget *)prefs_gadgets[7])->UserData=&(prefs_pal.type);
  945.     prefs_list.gadget->NextGadget=0;
  946.     req->ar_lastgadget=prefs_list.gadget;
  947. }
  948.  
  949. void __saveds __asm layout_buttons(register __a5 struct AMIS_Requester *req)
  950. {
  951.     long a;
  952.     a=((req->ar_width)-22-3*(req->ar_groupwidth))/2;
  953.     prefs_load.left=(req->ar_groupwidth)+14+a;
  954.     prefs_save.left=2*(req->ar_groupwidth)+14+2*a;
  955. }
  956.  
  957. int TextChar(char sign)
  958. {
  959.     return ((((sign>='0')&&(sign<='9'))||
  960.                 ((sign>='A')&&(sign<='Z'))||
  961.                 ((sign>='a')&&(sign<='z'))||
  962.                 (sign=='_')) ? TRUE : FALSE);
  963. }
  964.  
  965. void __stdargs _XCEXIT(long l)
  966. {
  967. }
  968.